package cn.exrick.xboot.modules.transport.controller;

import cn.exrick.xboot.common.utils.BeanConverterUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.transport.entity.BigCustomerUsers;
import cn.exrick.xboot.modules.transport.vo.BigCustomerUsersVo;
import cn.exrick.xboot.modules.transport.param.QueryBigCustomerUsersParam;
import cn.exrick.xboot.modules.transport.param.EditBigCustomerUsersParam;
import cn.exrick.xboot.modules.transport.service.BigCustomerUsersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author gj
 */
@Slf4j
@RestController
@Api(tags = "大客户与用户关联信息管理接口")
@RequestMapping("/xboot/bigCustomerUsers")
@Transactional
public class BigCustomerUsersController {

    @Autowired
    private BigCustomerUsersService bigCustomerUsersService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<BigCustomerUsersVo> get(@PathVariable Long id) {
        return new ResultUtil<BigCustomerUsersVo>().setData(bigCustomerUsersService.getBigCustomerUsersById(id));
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "绑定新增数据")
    public Result<BigCustomerUsersVo> save(@RequestBody EditBigCustomerUsersParam editBigCustomerUsersParam) {

        if (bigCustomerUsersService.saveCustomerUsers(editBigCustomerUsersParam)) {
            return new ResultUtil<BigCustomerUsersVo>().setData(BeanConverterUtil.convertBean(editBigCustomerUsersParam, BigCustomerUsersVo.class));
        }
        return new ResultUtil<BigCustomerUsersVo>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestBody Long[] ids) {

        for (Long id : ids) {
            bigCustomerUsersService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<BigCustomerUsersVo>> getByCondition(QueryBigCustomerUsersParam param,
                                                            PageVo page){
        return new ResultUtil<IPage<BigCustomerUsersVo>>().setData(bigCustomerUsersService.getByCondition(PageUtil.initMpPage(page),param));
    }
}
